<?php
namespace Admin\Controller;
use Think\Controller;

class FinanceController extends \Org\Util\AdminController {


    public function _initialize() {
        if (empty($_SESSION['finance'])) {
            $this->login();
            die;
        }
    }

    /**
     * 权限验证标志位,默认false
     * false:所有action都需要权限验证;
     * true:所有action都不需要权限验证;
     * array:array中的action不需要权限验证，其余需要。
     */
    protected $notAuthority = false;

    public function login() {
        $sys = D('system')->find(1);
        if (!empty($_POST['password'])) { 
            if (!password_verify($_POST['password'],$sys['financepassword'])) {
                $this->error('密码错误!');
            } else {
                $_SESSION['finance'] = 1;
                return $this->list();
            }
        }
        if (!empty($_POST['oldpassword'])) {
            if (!password_verify($sys['financepassword'],$_POST['oldpassword'])) {
                $this->error('密码错误!');
            }
            if ($_POST['newpassword'] != $_POST['newpassword2']) {
                $this->error('两次输入密码不同');
            }
            $financepassword = password_hash($_POST['newpassword'], PASSWORD_BCRYPT);
            $model = D('system');
            $model->financepassword = $financepassword;
            $model->where('id=1')->save(); 
            $this->success('保存成功');
        }
        $this->display('login');
    }


    /**
     * 创建之后做的事情
     */
    public function interfaceAfterCreate($id) {
    }

    /**
     * 删除之前做的事情
     */
    public function interfaceBeforeDelete($id) {
    }

    /**
     * 更新之前做的事情
     */
    public function interfaceBeforeUpdate($id) {
    }

    /**
     * 父类中已经有典型的CRUD操作并预留入口
     * 如有较大改动则覆盖一下
     */
    public function create() {
        parent::create();
    }

    public function delete() {
        parent::delete();
    }

    public function update($id) {
        parent::update($id);
    }

    public function list() {
        parent::list();
    }

    public function detail($id = 0) {
        parent::detail($id);
    }

    public function import() {
        parent::import();
    }

    public function export() {
        parent::export();
    }

    public function guiderinlist() {
        if (isset($_GET['searchMap']['location'])) {
            $map['user.location'] = array('like', '%'.$_GET['searchMap']['location'].'%');
        }
        if (!empty($_GET['searchMap']['Start'])) {
            $map['customized_guider.start_time'] = array('between', strtotime($_GET['searchMap']['Start']).','.strtotime($_GET['searchMap']['End']));
        }
        $map['finance.order_type'] = 'CustomizedGuider';
        $list = D('finance')
            ->field('finance.id, finance.order_type, finance.order_id, finance.create_time, sum(finance.amount) amount')
            ->join('customized_guider on finance.order_id = customized_guider.id')
            ->join('user on user.id = customized_guider.guider_user_id')
            ->where($map)
            ->group('finance.order_id')
            ->select();
       // dump(D('finance')->getLastsql());die;
        // dump(D('finance')->getLastsql());die;
        $this->assign('list', $list);
        $this->display('guiderlist');
    }
    public function guilderindetail() {
        if (isset($_GET['searchMap']['location'])) {
            $map['user.location'] = array('like', '%'.$_GET['searchMap']['location'].'%');
        }
        if (!empty($_GET['searchMap']['Start'])) {
            $map['customized_guider.start_time'] = array('between', strtotime($_GET['searchMap']['Start']).','.strtotime($_GET['searchMap']['End']));
        }
        $gid = $_GET['id']; 
        $map['customized_guider.guider_user_id'] = array('eq', $gid);
        $map['finance.order_type'] = 'CustomizedGuider';
        $list = D('finance')
            ->field('finance.*')
            ->join('customized_guider on finance.order_id = customized_guider.id')
            ->join('user on user.id = customized_guider.guider_user_id')
            ->where($map)
            ->select();
        $this->assign('list', $list);
        $this->display('guiderdetail');
    }

    public function custominlist() {
        if (isset($_GET['searchMap']['location'])) {
            $map['region.name'] = array('like', '%'.$_GET['searchMap']['location'].'%');
        }
        if (!empty($_GET['searchMap']['Start'])) {
            $map['customized_guider.start_time'] = array('between', strtotime($_GET['searchMap']['Start']).','.strtotime($_GET['searchMap']['End']));
        }
        $map['finance.order_type'] = 'CustomizedTravel';
        $list = D('finance')
            ->field('finance.id, finance.order_type, finance.order_id, finance.create_time,finance.amount as finance_amount,sum(finance.amount) as amount')
            ->join('customized_travel on finance.order_id = customized_travel.id')
            ->join('region on region.id = customized_travel.region_id')
            ->group('finance.order_id')
            ->where($map)
            ->select();
        //dump(D('finance')->getLastsql());die;
        $this->assign('list', $list);
        $this->display('customlist');
    }
    public function customindetail() {
        if (isset($_GET['searchMap']['location'])) {
            $map['region.name'] = array('like', '%'.$_GET['searchMap']['location'].'%');
        }
        if (!empty($_GET['searchMap']['Start'])) {
            $map['customized_travel.start_time'] = array('between', strtotime($_GET['searchMap']['Start']).','.strtotime($_GET['searchMap']['End']));
        }
        $id = $_GET['id'];
        //$map['customized_travel.club_user_id'] = array('eq', $id);
        $map['finance.order_id'] = array('eq', $id);
        $map['finance.order_type'] = 'CustomizedTravel';
        $list = D('finance')
            ->field('finance.*,customized_travel.state,customized_travel.name')
            ->join('customized_travel on finance.order_id = customized_travel.id')
            ->join('region on region.id = customized_travel.region_id')
            ->where($map)
            ->select();
        $this->assign('list', $list);
        $this->display('customdetail');
    }
    
    public function fundinginlist() {
        if (isset($_GET['searchMap']['type'])) {
            $map['funding.funding_type'] = array('eq', $_GET['searchMap']['type']);
        }
        if (isset($_GET['searchMap']['location'])) {
            $map['user.location'] = array('like', '%'.$_GET['searchMap']['location'].'%');
        }
        if (!empty($_GET['searchMap']['Start'])) {
            $map['funding_order.start_time'] = array('between', strtotime($_GET['searchMap']['Start']).','.strtotime($_GET['searchMap']['End']));
        }
        $map['finance.order_type'] = 'FundingOrder';
        $list = D('finance')
            ->field('finance.id, finance.order_type, finance.order_id, finance.create_time, sum(finance.amount) as amount')
            ->join('funding_order on funding_order.id = finance.order_id')
            ->join('funding on funding.id = funding_order.funding_id')
            ->join('user on user.id = finance.user_id')
            ->group('funding_order.funding_id')
            ->where($map)
            ->select();
        $this->assign('list', $list);
        $this->display('fundinglist');
    }
    public function fundingindetail() {
        if (isset($_GET['searchMap']['type'])) {
            $map['funding.funding_type'] = array('eq', $_GET['searchMap']['type']);
        }
        if (isset($_GET['searchMap']['location'])) {
            $map['user.location'] = array('like', '%'.$_GET['searchMap']['location'].'%');
        }
        if (!empty($_GET['searchMap']['Start'])) {
            $map['funding_order.create_time'] = array('between', strtotime($_GET['searchMap']['Start']).','.strtotime($_GET['searchMap']['End']));
        }
        $id = $_GET['id'];
        $map['finance.order_id'] = array('eq', $id);
        $map['finance.order_type'] = 'FundingOrder';
        $list = D('finance')
            ->field('finance.*')
            ->join('funding_order on funding_order.id = finance.order_id')
            ->join('funding on funding.id = funding_order.funding_id')
            ->join('user on user.id = finance.user_id')
            ->where($map)
            ->select();
        //dump(D('finance')->getLastsql());die;
        $this->assign('list', $list);
        $this->display('fundingdetail');
    }
    
    public function guideroutlist() {
        if (isset($_GET['searchMap']['location'])) {
            $map['user.location'] = array('like', '%'.$_GET['searchMap']['location'].'%');
        }
        if (!empty($_GET['searchMap']['Start'])) {
            $map['customized_guider.start_time'] = array('between', strtotime($_GET['searchMap']['Start']).','.strtotime($_GET['searchMap']['End']));
        }
        $map['finance.order_type'] = 'CustomizedGuider';
        $list = D('finance')
            ->field('finance.id, finance.order_type, finance.order_id, finance.create_time, sum(finance.amount) as amount')
            ->join('customized_guider on finance.order_id = customized_guider.id')
            ->join('user on user.id = customized_guider.guider_user_id')
            ->where($map)
            ->group('finance.order_id')
            ->select();
        $this->assign('list', $list);
        //dump(D('finance')->getLastsql());die;
        $this->display('guideroutlist');
    }
    public function guilderoutdetail() {
        if (isset($_GET['searchMap']['location'])) {
            $map['user.location'] = array('like', '%'.$_GET['searchMap']['location'].'%');
        }
        if (!empty($_GET['searchMap']['Start'])) {
            $map['customized_guider.start_time'] = array('between', strtotime($_GET['searchMap']['Start']).','.strtotime($_GET['searchMap']['End']));
        }
        $gid = $_GET['id'];
        $map['customized_guider.guider_user_id'] = array('eq', $gid);
        //$map['finance.order_id'] = array('eq', $gid);
        $map['finance.order_type'] = 'CustomizedGuider';
        $list = D('finance')
            ->field('finance.*')
            ->join('customized_guider on finance.order_id = customized_guider.id')
            ->join('user on user.id = customized_guider.guider_user_id')
            ->where($map)
            ->select();
        $this->assign('list', $list);
        $this->display('guideroutdetail');
    }

    public function customoutlist() {
        if (isset($_GET['searchMap']['location'])) {
            $map['region.name'] = array('like', '%'.$_GET['searchMap']['location'].'%');
        }
        if (!empty($_GET['searchMap']['Start'])) {
            $map['customized_guider.start_time'] = array('between', strtotime($_GET['searchMap']['Start']).','.strtotime($_GET['searchMap']['End']));
        }
        $map['finance.order_type'] = 'CustomizedTravel';
        $list = D('finance')
            ->field('finance.id, finance.order_type, finance.order_id, finance.create_time, sum(finance.amount) amount')
            ->join('customized_travel on finance.order_id = customized_travel.id')
            ->join('region on region.id = customized_travel.region_id')
            ->group('finance.order_id')
            ->where($map)
            ->select();
        $this->assign('list', $list);

        $this->display('customoutlist');
    }
    public function customoutdetail() {
        if (isset($_GET['searchMap']['location'])) {
            $map['region.name'] = array('like', '%'.$_GET['searchMap']['location'].'%');
        }
        if (!empty($_GET['searchMap']['Start'])) {
            $map['customized_travel.start_time'] = array('between', strtotime($_GET['searchMap']['Start']).','.strtotime($_GET['searchMap']['End']));
        }
        $id = $_GET['id'];
        $map['customized_travel.club_user_id'] = array('eq', $id);
        $map['finance.order_type'] = 'CustomizedTravel';
        $list = D('finance')
            ->field('finance.*, customized_travel.state,customized_travel. NAME')
            ->join('customized_travel on finance.order_id = customized_travel.id')
            ->join('region on region.id = customized_travel.region_id')
            ->where($map)
            ->select();
        $this->assign('list', $list);
        $this->display('customoutdetail');
    }
    
    public function fundingoutlist() {
        if (isset($_GET['searchMap']['type'])) {
            $map['funding.funding_type'] = array('eq', $_GET['searchMap']['type']);
        }
        if (isset($_GET['searchMap']['location'])) {
            $map['user.location'] = array('like', '%'.$_GET['searchMap']['location'].'%');
        }
        if (!empty($_GET['searchMap']['Start'])) {
            $map['funding_order.start_time'] = array('between', strtotime($_GET['searchMap']['Start']).','.strtotime($_GET['searchMap']['End']));
        }
        $map['finance.order_type'] = 'FundingOrder';
        $list = D('finance')
            ->field('finance.id, finance.order_type, finance.order_id, finance.create_time, sum(finance.amount) amount')
            ->join('funding_order on funding_order.id = finance.order_id')
            ->join('funding on funding.id = funding_order.funding_id')
            ->join('user on user.id = finance.user_id')
            ->group('funding.id')
            ->where($map)
            ->select();
        $this->assign('list', $list);
        $this->display('fundingoutlist');
    }
    public function fundingoutdetail() {
        if (isset($_GET['searchMap']['type'])) {
            $map['funding.funding_type'] = array('eq', $_GET['searchMap']['type']);
        }
        if (isset($_GET['searchMap']['location'])) {
            $map['user.location'] = array('like', '%'.$_GET['searchMap']['location'].'%');
        }
        if (!empty($_GET['searchMap']['Start'])) {
            $map['funding_order.create_time'] = array('between', strtotime($_GET['searchMap']['Start']).','.strtotime($_GET['searchMap']['End']));
        }
        $id = $_GET['id'];
        $map['finance.order_id'] = array('eq', $id);
        $map['finance.order_type'] = 'FundingOrder';
        $list = D('finance')
            ->field('finance.*')
            ->join('funding_order on funding_order.id = finance.order_id')
            ->join('funding on funding.id = funding_order.funding_id')
            ->join('user on user.id = finance.user_id')
            ->where($map)
            ->select();
        $this->assign('list', $list);
        $this->display('fundingdetail');
    }

 //   public function guideroutlist() {
 //       $_GET['flag'] = 'out';
 //       $this->guiderinlist();
 //   }
 //   public function customoutlist() {
 //       $_GET['flag'] = 'out';
 //       $this->custominlist();
 //   }
 //   public function fundingoutlist() {
 //       $_GET['flag'] = 'out';
 //       $this->fundinginlist();
 //   }
    public function totallist() {
        $map['finance.amount'] = array('egt', 0);
         $inlist = D('finance')
            ->field('finance.order_type, sum(finance.amount) sum')
            ->group('order_type')
            ->where($map)
            ->select();
        foreach ($inlist as $val) {
            $ret[$val['order_type']]['in'] = $val['sum'];
        }
        $map['finance.amount'] = array('lt', 0);
         $inlist = D('finance')
            ->field('finance.order_type, sum(finance.amount) sum')
            ->group('order_type')
            ->where($map)
            ->select();
        foreach ($inlist as $val) {
            $ret[$val['order_type']]['out'] = $val['sum'];
            $ret[$val['order_type']]['res'] = $ret[$val['order_type']]['out'] + $ret[$val['order_type']]['in'];
        }
        $this->assign('ret', $ret);
        $this->display('totallist');
    }
}
